﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Reflection;
using System.Threading;
using TradePublish.Common;
using TradePublish.Entity;

namespace TradePublish.SysThread.Grasp
{
	class AlibabaManager
	{
        private ILog log = null;

        public AlibabaManager()
        {
            this.log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        }

        public List<ApplyUserContract> graspContract(string keyword)
        {
            List<ApplyUserContract> rtn = new List<ApplyUserContract>();
            Dictionary<string, string> param = new Dictionary<string, string>();
            param.Add("keywords", keyword);
            param.Add("city", "");
            param.Add("province", "");
            param.Add("n", "y");
            string html = null;
            int exceptionCount = 0;
            while (true)
            {
                try
                {
                    html = new GetData().GetResponseForAlibaba("http://search.china.alibaba.com/company/company_search.htm", param);
                    if (html == null)
                    {
                        throw new Exception("%%%%%%返回null");
                    }
                    break;
                }
                catch (Exception ee)
                {
                    exceptionCount++;
                    Thread.Sleep(20000);
                    this.log.Error(string.Concat(new object[] { "=====第", exceptionCount, "次访问Alibaba发生异常.keyword=", keyword, ee.Message, "<br/>" }));
                    if (exceptionCount > 2)
                    {
                        html = null;
                        break;
                    }
                }
            }

            //解析
            if (html == null || html.IndexOf("抱歉，没有找到符合") > 0)
            {
                ApplyUserContract applyUserContract = new ApplyUserContract();
                applyUserContract.applyUserName = keyword;
                applyUserContract.wangwangNo = "";
                applyUserContract.isChengxintong = "0";
                applyUserContract.wangwangHtml = "【标记！无旺旺号】";
                rtn.Add(applyUserContract);
                return rtn;
            }
            String isChengxintong = "0";

            int index = 0;
            while (html.IndexOf("memberId=\"", index) > 0)
            {
                index = html.IndexOf("memberId=\"", index) + "memberId=\"".Length;
                String splitHtml = html.Substring(index, html.IndexOf("</ul>", index) - index);
                if (splitHtml.IndexOf("class=\"Wxb\"") > 0)
                    continue;//推广 
                isChengxintong = "0";
                if (splitHtml.IndexOf("cxt-mumber-c") > 0)
                {
                    isChengxintong = "1";
                }

                String wwNo = splitHtml.Substring(0, splitHtml.IndexOf("\""));
                String h2HTML = splitHtml.Substring(splitHtml.IndexOf("<h2>"), splitHtml.IndexOf("</h2>") - splitHtml.IndexOf("<h2>"));
                if (h2HTML.IndexOf(keyword) < 0)
                {
                    continue;
                }
                //看看是否是部分完全匹配
                ApplyUserContract applyUserContract = new ApplyUserContract();
                applyUserContract.applyUserName = keyword;
                applyUserContract.wangwangNo = wwNo;
                applyUserContract.wangwangHtml = "<a href='http://amos1.taobao.com/msg.ww?v=2&uid=" + Uri.EscapeDataString(wwNo) + "&s=1'>旺旺联系</a>";
                applyUserContract.isChengxintong = isChengxintong;
                rtn.Add(applyUserContract);
            }
            if (rtn == null || rtn.Count == 0)
            { 
                ApplyUserContract applyUserContract = new ApplyUserContract();
                applyUserContract.applyUserName = keyword;
                applyUserContract.wangwangNo = "";
                applyUserContract.isChengxintong = "0";
                applyUserContract.wangwangHtml = "【标记！无旺旺号】";
                rtn.Add(applyUserContract);
            }
            return rtn;

        }
 
	}
}
